package znzz.quartz.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import znzz.quartz.service.JobService;
import znzz.quartz.vo.PageTrigger;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *
 *
 * Created by wangjwm on 2017/7/25.
 */
@Controller
@RequestMapping(value = "/scheduler")
public class SchedulerCtrl {

    private boolean init = false;

    @Autowired
    private JobService jobService;

    @RequestMapping("/show")
    public String show(HttpServletRequest request) {
        if(!init) {
            jobService.stopScheduler();
        }
        List<PageTrigger> triggers = jobService.getTriggersInfo();
        request.setAttribute("triggers", triggers);
        return "show";
    }

    @RequestMapping("/{name}/{group}/stop")
    public String stop(@PathVariable String name, @PathVariable String group) {
        jobService.stopJob(name, group);
        return "redirect:/scheduler/show";
    }

    @RequestMapping("/{name}/{group}/del")
    public String del(@PathVariable String name, @PathVariable String group) {
        jobService.delJob(name, group);
        return "redirect:/scheduler/show";
    }

    @RequestMapping("/{name}/{group}/{cron}/modify")
    public String modify(@PathVariable String name, @PathVariable String group, @PathVariable String cron) {
        jobService.modifyTrigger(name, group, cron);
        return "redirect:/scheduler/show";
    }

    @RequestMapping("/{name}/{group}/startNow")
    public String startNow(@PathVariable String name, @PathVariable String group) {
        jobService.startNowJob(name, group);
        return "redirect:/scheduler/show";
    }

    @RequestMapping("/{name}/{group}/resume")
    public String resume(@PathVariable String name, @PathVariable String group) {
        jobService.restartJob(name, group);
        return "redirect:/scheduler/show";
    }


}
